package com.niiwoo.civet.account.service.dubbo.trade;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.WithholdOrder;
import com.niiwoo.civet.account.dto.common.WithholdOrderDTO;
import com.niiwoo.civet.account.enums.OrderEndTypeEnum;
import com.niiwoo.civet.account.enums.WithholdOrderStatusEnum;
import com.niiwoo.civet.account.service.local.trade.WithholdFacade;
import com.niiwoo.civet.account.service.local.trade.WithholdOrderService;
import com.niiwoo.civet.account.service.local.trade.WithholdService;
import com.niiwoo.civet.account.service.trade.WithholdDubboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.support.Assert;

import java.util.Objects;

/**
 * Created by xiaoyingping on 2018/4/24.
 */
@Service(version = "1.0.0")
@Slf4j
public class WithholdDubboServiceImpl implements WithholdDubboService {

    @Autowired
    private WithholdFacade withholdFacade;
    @Autowired
    private WithholdService withholdService;
    @Autowired
    private WithholdOrderService withholdOrderService;

    @Override
    public WithholdOrderDTO queryWithholdOrderByApplyOrderId(Long applyOrderId) {
        WithholdOrder withholdOrder = withholdOrderService.getWithholdOrderByOrderId(applyOrderId);
        if(Objects.isNull(withholdOrder)){
            return null;
        }

        WithholdOrderDTO withholdOrderDTO = new WithholdOrderDTO();
        withholdOrderDTO.setRequestNo(withholdOrder.getRequestNo());
        withholdOrderDTO.setStatus(WithholdOrderStatusEnum.getEnumByCode(withholdOrder.getStatus()));
        withholdOrderDTO.setCreateTime(withholdOrder.getCreateTime());
        withholdOrderDTO.setSuccessTime(withholdOrder.getSuccessTime());
        withholdOrderDTO.setErrorCode(withholdOrder.getErrorCode());
        withholdOrderDTO.setErrorMessage(withholdOrder.getErrorMessage());
        withholdOrderDTO.setBankCardId(withholdOrder.getBankCardId());
        return withholdOrderDTO;
    }

    @Override
    public void queryLanMaoWithholdResultJob(){
        withholdFacade.queryLanMaoWithholdResultJob();
    }

    @Override
    public void handProcessWithholdResult(Long withholdOrderId, WithholdOrderStatusEnum withholdOrderStatus){
        Assert.notNull(withholdOrderId,"withholdOrderId不能为空");
        Assert.notNull(withholdOrderStatus,"withholdOrderStatus不能为空");

        log.info("handProcessWithholdResult start, withholdOrderId:{}, withholdOrderStatus:{}", withholdOrderId, withholdOrderStatus);
        if(WithholdOrderStatusEnum.SUCCESS == withholdOrderStatus){
            withholdService.processWithholdSuccess(withholdOrderId, OrderEndTypeEnum.SUCCESS);
        }else if(WithholdOrderStatusEnum.FAIL == withholdOrderStatus){
            withholdService.processWithholdFail(withholdOrderId,"9","手工处理为失败", OrderEndTypeEnum.SUCCESS);
        }else {
            log.info("withholdOrderStatus的值是{}，不能做处理");
        }
    }


}
